<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2023/1/12
 * Time: 15:49
 */

namespace app\admin\controller;

use think\Db;

class Giftbagyj extends Base
{
    /**
     * 礼包服务费管理首页
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $bgi_number = trim(input('bgi_number'), ' '); // 流水号
            $user_real_name = trim(input('user_real_name'), ' '); // 用户名
            $user_phone = trim(input('user_phone'), ' '); // 手机号
            $bgi_desc = trim(input('bgi_desc'));
            $start_time = input('start_time');
            $end_time = input('end_time');
            $bgi_type = input('bgi_type'); // 1 收入  2 支出

            $where = [];
            if(isset($bgi_number) && $bgi_number != ''){
                $where['bgi_number'] = ['like', '%' . $bgi_number . '%'];
            }
            if(isset($user_real_name) && $user_real_name != ''){
                $where['user_real_name'] = ['like', '%' . $user_real_name . '%'];
            }
            if(isset($user_phone) && $user_phone != ''){
                $where['user_phone'] = ['like', '%' . $user_phone . '%'];
            }
            if(isset($bgi_desc) && $bgi_desc != ''){
                $where['bgi_desc'] = ['like', '%' . $bgi_desc . '%'];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['bgi_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['bgi_time'] = ['>=', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['bgi_time'] = ['<=', $end_time];
            }
            if(isset($bgi_type) && $bgi_type != ''){
                if($bgi_type == 1){
                    $where['bgi_type'] = 1;
                }else{
                    $where['bgi_type'] = ['BETWEEN', [2, 3]];
                }
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_bo_giftbag_integral')
                ->join('up_u_user', 'bgi_user_id = user_id', 'LEFT')
                ->where($where)
                ->count('bgi_id');
            $lists = Db::table('up_bo_giftbag_integral')
                ->join('up_u_user', 'bgi_user_id = user_id', 'LEFT')
                ->field('bgi_id, bgi_type, bgi_number, bgi_desc, bgi_price, bgi_front, 
                bgi_black, bgi_time, user_real_name, user_phone')
                ->where($where)
                ->order('bgi_time desc, bgi_id desc')
                ->page($Nowpage, $limits)
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /**
     * 导出
     * @return \think\response\Json
     */
    public function outExcel(){
        set_time_limit(0);
        $bgi_ids = input('ids');

        $where = [];
        if($bgi_ids == -1){
            $bgi_number = trim(input('bgi_number'), ' '); // 流水号
            $user_real_name = trim(input('user_real_name'), ' '); // 用户名
            $user_phone = trim(input('user_phone'), ' '); // 手机号
            $bgi_desc = trim(input('bgi_desc'));
            $start_time = input('start_time');
            $end_time = input('end_time');
            $bgi_type = input('bgi_type'); // 1 收入  2 支出

            if(isset($bgi_number) && $bgi_number != ''){
                $where['bgi_number'] = ['like', '%' . $bgi_number . '%'];
            }
            if(isset($user_real_name) && $user_real_name != ''){
                $where['user_real_name'] = ['like', '%' . $user_real_name . '%'];
            }
            if(isset($user_phone) && $user_phone != ''){
                $where['user_phone'] = ['like', '%' . $user_phone . '%'];
            }
            if(isset($bgi_desc) && $bgi_desc != ''){
                $where['bgi_desc'] = ['like', '%' . $bgi_desc . '%'];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['bgi_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['bgi_time'] = ['>=', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['bgi_time'] = ['<=', $end_time];
            }
            if(isset($bgi_type) && $bgi_type != ''){
                if($bgi_type == 1){
                    $where['bgi_type'] = 1;
                }else{
                    $where['bgi_type'] = ['BETWEEN', [2, 3]];
                }
            }
        }else{
            if($bgi_ids == '') return json(['code' => 100, 'msg' => '请选择需要导出的数据']);
            $where['bgi_id'] = ['IN', $bgi_ids];
        }

        $lists = Db::table('up_bo_giftbag_integral')
            ->join('up_u_user', 'bgi_user_id = user_id', 'LEFT')
            ->field('bgi_id, bgi_type, bgi_number, bgi_desc, bgi_price, bgi_front, 
                bgi_black, bgi_time, user_real_name, user_phone')
            ->where($where)
            ->order('bgi_time desc')
            ->select();
        foreach($lists as $k => $v){
            // 类型
            if($v['bgi_type'] == 1){
                $lists[$k]['bgi_type'] = '收入';
            }else{
                $lists[$k]['bgi_type'] = '支出';
            }
            // 服务费
            $lists[$k]['bgi_price'] = "\t" . $v['bgi_price'] . "\t";
            // 变动前服务费
            $lists[$k]['bgi_front'] = "\t" . $v['bgi_front'] . "\t";
            // 变动后服务费
            $lists[$k]['bgi_black'] = "\t" . $v['bgi_black'] . "\t";
            // 手机号
            $lists[$k]['user_phone'] = "\t" . $v['user_phone'] . "\t";
        }
        $cellname = [
            ['user_real_name', '用户', 10, 'LEFT'],
            ['user_phone', '用户手机号', 20, 'LEFT'],
            ['bgi_type', '类型', 10, 'LEFT'],
            ['bgi_desc', '描述', 23, 'LEFT'],
            ['bgi_number', '流水号', 25, 'LEFT'],
            ['bgi_front', '变动前服务费', 20, 'LEFT'],
            ['bgi_price', '服务费', 15, 'LEFT'],
            ['bgi_black', '变动后服务费', 20, 'LEFT'],
            ['bgi_time', '记录时间', 20, 'LEFT']
        ];
        $res = exportExcelZhy('礼包服务费明细', 'Giftbag_integral', $cellname, $lists);
        return json($res);
    }
}